package com.xuanwei.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanwei.core.annos.Positive;
import com.xuanwei.core.entity.vo.*;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwCourierCompanyService;
import com.xuanwei.core.service.IXwOrderCommentService;
import com.xuanwei.core.service.IXwOrderService;
import com.xuanwei.dao.Do.OrderDo;
import com.xuanwei.dao.entity.XwCourierCompany;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.time.LocalDate;
import java.util.List;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/7/17 15:57
 **/
@RestController
@RequestMapping("api/order")
@Validated
public class OrderController extends BasicController {

    private IXwOrderService orderService;

    private IXwCourierCompanyService courierCompanyService;

    private IXwOrderCommentService orderCommentService;

    public OrderController(IXwOrderService orderService,
                           IXwCourierCompanyService courierCompanyService,
                           IXwOrderCommentService orderCommentService) {
        this.orderService = orderService;
        this.orderCommentService = orderCommentService;
        this.courierCompanyService = courierCompanyService;
    }

    @RequestMapping(value = "orderList.do", method = RequestMethod.GET)
    public ResultVO orderList(Integer type,
                              String orderNo, String nickname,
                              String receiveName, String receivePhone,
                              @RequestParam(required = false) LocalDate startTime,
                              @RequestParam(required = false) LocalDate endTime) {
        Page<OrderDo> page = getPage();

        Page<OrderListVo> voPage = orderService.businessOrderList(type, orderNo,
                nickname, receiveName, receivePhone, startTime, endTime, page);

        return ResultVO.successResult(voPage);
    }

    @RequestMapping(value = "refundOrderList.do", method = RequestMethod.GET)
    public ResultVO refundOrderList(Integer type,
                              String orderNo, String nickname,
                              String receiveName, String receivePhone,
                              @RequestParam(required = false) LocalDate startTime,
                              @RequestParam(required = false) LocalDate endTime) {
        Page<OrderDo> page = getPage();
        Page<OrderListVo> voPage = orderService.refundOrderList(type, orderNo,
                nickname, receiveName, receivePhone, startTime, endTime, page);

        return ResultVO.successResult(voPage);
    }

    @RequestMapping(value = "send.do", method = RequestMethod.GET)
    public ResultVO send(Integer companyId,
                         @Length(min = 8, message = "快递单号错误") String courierNo,
                         @Min(value = 1, message = "订单ID格式错误") Integer orderId) {
        orderService.send(orderId, companyId, courierNo);

        return ResultVO.successResult("发货成功");
    }

    @RequestMapping(value = "detail.do", method = RequestMethod.GET)
    public ResultVO detail(@Min(value = 1, message = "订单ID格式错误") Integer orderId) {

        OrderDetailVo detail = orderService.detail(orderId);

        return ResultVO.successResult(detail);
    }


    @RequestMapping(value = "courierInfo.do", method = RequestMethod.GET)
    public ResultVO courierInfo(@Min(value = 1, message = "订单ID格式错误") Integer orderId) {

        CourierInfoVo courierInfo = orderService.findCourierInfo(orderId);

        return ResultVO.successResult(courierInfo);
    }


    @RequestMapping(value = "allCourierCompany.do", method = RequestMethod.GET)
    public ResultVO allCourierCompany() {
        List<XwCourierCompany> list = courierCompanyService.list();
        return ResultVO.successResult(list);
    }

    @RequestMapping(value = "queryComment.do", method = RequestMethod.GET)
    public ResultVO queryComment(@Positive(message = "ID格式错误") Integer orderId) {
        OrderCommentVo detail = orderCommentService.findDetail(orderId);
        return ResultVO.successResult(detail);
    }

    @RequestMapping(value = "refundDetail.do", method = RequestMethod.GET)
    public ResultVO refundDetail(@Positive(message = "ID格式错误") Integer orderId) {
        RefundDetail detail = orderService.findRefundDetail(orderId);
        return ResultVO.successResult(detail);
    }

    @RequestMapping(value = "handleAgree.do", method = RequestMethod.GET)
    public ResultVO handleAgree(@Positive(message = "ID格式错误") Integer orderId) {
        orderService.handleAgree(orderId);
        return ResultVO.successResult();
    }

    @RequestMapping(value = "handleReject.do", method = RequestMethod.GET)
    public ResultVO handleReject(@Positive(message = "ID格式错误") Integer orderId,
                                 @NotBlank(message = "原因不能为空") String reason) {
        orderService.handleReject(orderId,reason);
        return ResultVO.successResult();
    }
}
